//
//  Auction_Dialogs_View_Wx.m
//  ZITHER
//
//  Created by weixin on 2019/5/30.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Auction_Dialogs_View_Wx.h"

@interface Auction_Dialogs_View_Wx()
@property (nonatomic, strong) UIView *dimBackgroundView;/**< 半透明黑色背景 */
@property (nonatomic, strong) UIWindow *window;
@end



@implementation Auction_Dialogs_View_Wx

- (void)show
{
    [self addToKeyWindow];
    [self showAnimationWithCompletion:nil];
}

- (void)hide
{
    [self hideAnimationWithCompletion:^(BOOL finished) {
        [self removeFromKeyWindow];
    }];
}


#pragma mark - private method

- (void)addToKeyWindow
{
    if (!self.superview) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow addSubview:self];
        [self addSubview:self.dimBackgroundView];
//        [self addSubview:self.tbv];
//        [self addSubview:self.cancelBtn];
//        [self addSubview:self.head];
//        [self.tbv mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(self.mas_bottom).offset(-50);
//            make.left.right.equalTo(self).offset(0);
//            make.height.mas_equalTo(self.model.officialPrices.count*50);
//        }];
//        [self.head mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(self.tbv.mas_top).offset(0);
//            make.left.right.equalTo(self).offset(0);
//            make.height.mas_equalTo(110);
//        }];
    }
}

- (void)removeFromKeyWindow
{
    if (self.superview) {
        [self removeFromSuperview];
    }
}

- (void)showAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0.3;
    } completion:completion];
}

- (void)hideAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0;
    } completion:completion];
}

#pragma mark - getter
- (UIView *)dimBackgroundView
{
    if (!_dimBackgroundView) {
        _dimBackgroundView = [[UIView alloc] init];
        _dimBackgroundView.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        _dimBackgroundView.backgroundColor = [UIColor blackColor];
        _dimBackgroundView.alpha = 0;
        
        // 添加手势监听
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [_dimBackgroundView addGestureRecognizer:tap];
    }
    return _dimBackgroundView;
}

@end
